/*
 * 文件名               :  UserLoginThread.java
 * 用途                    :  监听用户登录的线程
 * 最后修改日期:  $LastChangedDate: 2009-09-14 13:13:18 +0000 (Mon, 14 Sep 2009) $
 * 最后修改者     :  $LastChangedBy: wendal1985@gmail.com $
 * 创建者               :  Wendal Chen <wendal1985@gmail.com>
 */
package com.wendal.java.preproxy.bridge;

import java.io.IOException;
import java.net.Socket;

import com.wendal.java.preproxy.bean.NoticeMessageBean;
import com.wendal.java.preproxy.server.SocketHandlerFinder;
import com.wendal.java.preproxy.server.SocketListener;
import com.wendal.java.preproxy.util.Toolkit;

public class UserLoginThread extends Thread {
	private Socket socket;

	private static final ByteArrayHandler handler = new ByteArrayMuteHandler();

	public UserLoginThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			SocketHandlerFinder socketHandlerFinder = new SocketHandlerFinder();
			BridgeConnector bridgeConnector = new BridgeConnector(socket,
					handler, socketHandlerFinder);

			SocketListener listener = SocketListener.getSocketListener(
					bridgeConnector, socketHandlerFinder);
			listener.start();
			NoticeMessageBean messageBean = new NoticeMessageBean();
			messageBean.setData("你已成功登陆! 外网访问地址为 http://"
					+ Toolkit.getLocalhostIp() + ":" + listener.getPort());
			bridgeConnector.transfer(messageBean);
			
			System.out.println("有一个用户成功登陆! " + socket);
		} catch (IOException e1) {
			Toolkit.close(socket);
			e1.printStackTrace();
		}
	}
}